package com.bluishoul.bean;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import my.db.QueryHelper;
import my.mvc.IUser;
import my.mvc.RequestContext;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

@SuppressWarnings("serial")
public class User extends Pojo implements IUser {
	public final static transient User INSTANCE = new User();
	public final static int REG_TYPE_NORMAL = 0;
	public final static int REG_TYPE_QQ = 1;
	public final static int REG_TYPE_WEIBO = 2;

	private String username;
	private String pwd;
	private String email;
	private String accessToken;
	private String openId;
	private String nickName;
	private String gender;
	private String gravatar;
	private int type;
	private byte role;
	private Timestamp create_time;

	public boolean CanAdmin(User user) {
		if (user != null) {
			if (user.getId() == this.getId() || this.IsSuperior())
				return true;
		}
		return false;
	}

	/**
	 * 返回当前登录用户的资料
	 * 
	 * @param req
	 * @return
	 */
	public static User GetLoginUser(HttpServletRequest req) {
		User loginUser = (User) req.getAttribute(G_USER);
		if (loginUser == null) {
			IUser cookie_user = RequestContext.get().getUserFromCookie();
			if (cookie_user == null)
				return null;
			User user = User.Read(cookie_user.getId());
			if (user != null
					&& StringUtils.equalsIgnoreCase(user.getPwd(),
							cookie_user.getPwd())) {
				req.setAttribute(G_USER, user);
				return user;
			}
		}
		return loginUser;
	}

	public User hasSame() {
		if (StringUtils.isNotBlank(username)) {
			String sql = "SELECT * FROM menu_user WHERE username=?";
			return QueryHelper.read(User.class, sql, username);
		}
		return null;
	}

	public long register3rd() {
		this.role = ROLE_GENERAL;
		return this.Save();
	}

	public static User Read(long id) {
		return (User) INSTANCE.Get(id);
	}

	public User login3rd() {
		String sql = "SELECT * FROM menu_user WHERE accessToken=? or openId=?";
		User user = QueryHelper.read(User.class, sql, this.accessToken,
				this.openId);
		return user;
	}

	public User login() {
		if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(pwd)) {
			String sql = "SELECT * FROM menu_user WHERE username=? && pwd=?";
			return QueryHelper.read(User.class, sql, username,
					DigestUtils.shaHex(pwd));
		}
		return null;
	}

	public long register() {
		if (StringUtils.isBlank(getUsername()) || StringUtils.isBlank(getPwd())
				|| StringUtils.isBlank(getEmail()))
			return 0;
		this.role = ROLE_GENERAL;
		this.type = REG_TYPE_NORMAL;
		this.pwd = DigestUtils.shaHex(pwd);
		return this.Save();
	}

	public boolean updateUserInfo() {
		if (StringUtils.isBlank(getUsername()) || StringUtils.isBlank(getPwd())
				|| StringUtils.isBlank(getEmail()))
			return false;
		String sql = "UPDATE menu_user SET username=?,pwd=?,emal=? WHERE id=?";
		return Evict(QueryHelper.update(sql, username,
				DigestUtils.sha256Hex(pwd), getId()) == 1);
	}

	/**
	 * 判断是否为后台用户
	 * 
	 * @return
	 */
	public boolean IsSuperior() {
		return role >= ROLE_EDITOR;
	}

	public boolean IsAdmin() {
		return role >= ROLE_ADMIN;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getGravatar() {
		return gravatar;
	}

	public void setGravatar(String gravatar) {
		this.gravatar = gravatar;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public byte getRole() {
		return role;
	}

	public void setRole(byte role) {
		this.role = role;
	}

	public Timestamp getCreate_time() {
		return create_time;
	}

	public void setCreate_time(Timestamp create_time) {
		this.create_time = create_time;
	}

	@Override
	public String TableName() {
		return "menu_user";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public String getPwd() {
		return pwd;
	}

	@Override
	public boolean IsBlocked() {
		return false;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
}
